const api = require('../../utils/api.js');

Page({
    data: {
        isBindMessage: false
    },
    onLoad(options) {

    },
    onShow() {
        this.getUserInfo();
    },

    getUserInfo() {
        api.post('/user/info')
            .then(res => {
                this.setData({
                    isBindMessage: res.isWxBound === 1 ? true : false
                });
            })
    },
    bindMessage(e) {
        // console.log(e.detail)
        if(e.detail.value) {
            wx.showModal({
                title: '提示',
                content: `开启消息推送需要先授权公众号，是否去操作？`,
                success: res => {
                    if(res.confirm) {
                        wx.navigateTo({
                            url: '/pages/bindWeixin/bindWeixin'
                        })
                    } else {
                        this.setData({
                            isBindMessage: false
                        })
                    }
                }
            })
        } else {
            wx.showModal({
                title: '提示',
                content: `关闭消息推送后您将无法接受到消息提醒，可能会错过重要消息。您是否确定关闭？`,
                success: res => {
                    if(res.confirm) {
                        api.post('/user/unbindUserAuth')
                            .then(() => {
                                wx.showToast({
                                    title: '消息推送已关闭',
                                    icon: 'success'
                                });
                                this.setData({
                                    isBindMessage: false
                                })
                            })
                    } else {
                        this.setData({
                            isBindMessage: true
                        })
                    }
                }
            })
        }
    }
})